package com.ts.common.utils;

import java.security.SecureRandom;

/**
 * @Author:lk
 * @Date: 2025/5/16  22:08
 */
public class CodeUtil {
    // 定义数字字符集
    private static final String  DIGITS = "0123456789";
    private static final SecureRandom random = new SecureRandom();

    /**
     * 生成指定长度的随机数字验证码
     *
     * @param length 验证码的长度
     * @return 生成的验证码
     */
    public static String generateComplexNumberCode(int length) {
        StringBuilder code = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(DIGITS.length());
            // 确保不会连续生成相同的数字
            if (i > 0 && code.charAt(i - 1) == DIGITS.charAt(index)) {
                index = (index + 1) % DIGITS.length();
            }
            code.append(DIGITS.charAt(index));
        }
        return code.toString();
    }


}
